package com.thams.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.thams.entity.BasPart;
import com.thams.entity.ComputationUnit;
import com.thams.entity.Inventory;
import com.thams.entity.InventorySub;
import com.thams.entity.UserDefine;
import com.thams.service.i.IU8Service;
import com.thams.util.MessagePrinter;
import com.thams.util.ParseRequestUtils;

public class InventoryServlet extends HttpServlet {
	private static IU8Service service;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String forward = request.getParameter("forward");
		if (null == forward || "".equals(forward)) {
			MessagePrinter.printErrorMessage("请检查访问参数是否正确",request, response);
			return;
		}

		service = (IU8Service) WebApplicationContextUtils
				.getRequiredWebApplicationContext(
						request.getSession().getServletContext()).getBean(
						"u8Service");

		if (forward.equals("getInventoryNumber")) {
			getInventoryNumber(request, response);
			return;
		}
		if (forward.equals("addInventory")) {
			addInventory(request, response);
		}

		if(forward.equals("getComputationUnit")){
			getComputationUnitList(request, response);
		}
	}

	private void getComputationUnitList(HttpServletRequest request,
			HttpServletResponse response) {
		List<ComputationUnit> comList = service.getComputationUnitList();
		StringBuilder sb = new StringBuilder("[");
		for (int i = 0; i < comList.size(); i++) {
			ComputationUnit c = comList.get(i);
			sb.append(c.toJSON());
			if (i != comList.size() - 1) {
				sb.append(",");
			}
		}
		sb.append("]");
		MessagePrinter.printObject(sb.toString(),request, response);
		
	}

	private void addInventory(HttpServletRequest request,
			HttpServletResponse response) {
		Inventory inv = new Inventory();
		inv = (Inventory) ParseRequestUtils.parseRequest(request, inv);

		InventorySub invSub = new InventorySub();
		invSub = (InventorySub) ParseRequestUtils.parseRequest(request, invSub);

		BasPart basPart = new BasPart();
		basPart = (BasPart) ParseRequestUtils.parseRequest(request, basPart);
		try {
			service.insertInventory(inv, invSub,basPart);
			MessagePrinter.printSuccessMessage("添加存货成功",request, response);
		} catch (Exception e) {
			e.printStackTrace();
			MessagePrinter.printErrorMessage("添加存货出错,请检查是否包含空数据或者已存在数据!",request,
					response);
		}
	}

	private void getInventoryNumber(HttpServletRequest request,
			HttpServletResponse response) {
		String cinvCode = request.getParameter("cinvCode");
		String result = service.getInventoryNumber(cinvCode);
		if(result!=null){
			MessagePrinter.printSuccessMessage(result,request, response);
		}else{
			MessagePrinter.printErrorMessage("获取存货数量出错,请检查是否存在当前存货!",request,
					response);
		}
	}

}
